<template>
  <el-dialog
    v-loading="loading"
    :visible.sync="dialogVisible"
    :title="title"
    :close-on-click-modal="false"
    width="600px"
    @close="close"
  >
    <!--  html  -->
    <el-form v-if="couponDetail" :model="form" label-suffix="：" label-width="100px" size="small">
      <el-form-item label="券名称">{{ couponDetail.couponName }}</el-form-item>
      <el-form-item label="归属商户">{{ couponDetail.belongMerchantName }}</el-form-item>
      <el-form-item label="有效期">{{ couponDetail.startTime + ' ~ ' + couponDetail.endTime }}</el-form-item>
      <!--      <el-form-item label="库存">-->
      <!--        <span v-if="couponDetail.maxAmount===0">无限制</span>-->
      <!--        <span v-else> {{ couponDetail.maxAmount-couponDetail.issueAmount }}</span>-->
      <!--      </el-form-item>-->
      <el-form-item label="预制数">
        <span class="app-text-color-danger">{{ form.amount }}</span>
      </el-form-item>
    </el-form>
    <el-form v-if="cardDetail" :model="form" label-suffix="：" label-width="100px" size="small">
      <el-form-item label="卡名称">{{ cardDetail.cardName }}</el-form-item>
      <el-form-item label="归属商户">{{ cardDetail.belongMerchantName }}</el-form-item>
      <el-form-item label="有效期">{{ cardDetail.startTime + ' ~ ' + cardDetail.endTime }}</el-form-item>
      <el-form-item label="卡密">
        <el-radio-group v-model="form.needSecret">
          <el-radio :label="false">不需要生成卡密</el-radio>
          <el-radio :label="true">同时生成卡密</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="预制数">
        <span class="app-text-color-danger">{{ form.amount }}</span>
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button size="small" round @click="hide">取 消</el-button>
      <el-button size="small" round type="primary" @click="submit">确定并发起预制任务</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { createCouponPreJob, createCardPreJob } from '../../../api/pre'
import { couponDetail } from '../../../api/coupon'
import { cardDetail } from '../../../api/card'

export default {
  data() {
    return {
      // 配置
      title: '预制确认',
      resolve: null,
      reject: null,
      couponTypeOptions: {
        cash: '代金券',
        cash_wx_pay: '微信支付代金券',
        cash_wx_busi: '微信商家代金券',
        discount: '折扣券',
        discount_wx_busi: '微信商家折扣券',
        exchange: '兑换券'
      },
      // 状态
      dialogVisible: false,
      loading: false,
      // 数据
      form: {
        type: 'coupon',
        stockId: null,
        amount: null,
        needSecret: false
      },
      couponDetail: null,
      cardDetail: null
    }
  },
  computed: {},
  watch: {},
  beforeCreate() {
  },
  created() {
  },
  beforeMount() {
  },
  mounted() {

  },
  beforeUpdate() {
  },
  update() {
  },
  activated() {
  },
  deactivated() {
  },
  beforeDestroy() {
  },
  destroyed() {
  },
  methods: {
    // 弹框相关
    open(row) {
      row.needSecret = false
      this.form = JSON.parse(JSON.stringify(row))
      this.getDetail(row.stockId)
      this.show()
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
      })
    },

    show() {
      this.dialogVisible = true
    },

    hide() {
      this.resolve = null
      this.reject = null
      this.dialogVisible = false
    },

    close() {
      this.loading = false
    },

    /**
     * 数据组
     */
    // 获取详情
    getDetail() {
      if (this.form.type === 'coupon') {
        this.loading = true
        couponDetail({ couponId: this.form.stockId }).then(res => {
          if (!res.data) {
            this.$message.warning('查无此卡券数据')
            this.hide()
            return
          } else {
            const detail = res.data.prototype
            detail.availableTime = res.data.availableTime
            this.couponDetail = detail
            this.show()
          }
        }).finally(() => {
          this.loading = false
        })
      }
      if (this.form.type === 'card') {
        this.loading = true
        cardDetail({ cardId: this.form.stockId }).then(res => {
          if (!res.data) {
            this.$message.warning('查无此卡券数据')
            this.hide()
            return
          }
          const detail = res.data.prototype
          detail.availableTime = res.data.availableTime
          this.cardDetail = detail
          this.show()
        }).finally(() => {
          this.loading = false
        })
      }
    },
    /**
     * 按钮组
     */
    submit() {
      this.loading = true
      const params = Object.assign({}, this.form)
      if (this.form.type === 'coupon') {
        delete params.needSecret
        createCouponPreJob(params).then(res => {
          this.$notify({
            title: '成功',
            message: '创建预制任务成功',
            type: 'success',
            duration: 2000,
            offset: 40
          })
          this.hide()
        }).finally(() => {
          this.loading = false
        })
      }
      if (this.form.type === 'card') {
        createCardPreJob(params).then(res => {
          this.$notify({
            title: '成功',
            message: '创建预制任务成功',
            type: 'success',
            duration: 2000,
            offset: 40
          })
          this.hide()
        }).finally(() => {
          this.loading = false
        })
      }
    }
    /**
     * 状态组
     */
    /**
     * 方法组
     */
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>

</style>
